<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class News_Model extends CI_Model
{
    protected $_lang = '';
	function __construct() {
        parent::__construct();
    }
    public function initialise($lang)
    {
        if($lang!=''&&$lang!='vn')
            $this->_lang = '_'.$lang;
        else
            $this->_lang = '';
    }
    /*
     * @author : Vo Van Hieu
     * @method : get num rows news by category
     * @param  : $id_cat - ID category news
     *           $arr_search - arrary condition search
     * @date   : 12/11/2012
     * @modify :
     */
    function getNumNewsByCat($alias_cate,$arr_search=array()){
        $this->db->select('news.news_id');
        $this->db->join('category_news','news.category_news_id=category_news.category_news_id');
		 $this->db->where('('.$this->db->dbprefix('category_news').'.name_alias=\''.trim($alias_cate).'\' OR '.$this->db->dbprefix('category_news').'.name_alias_en = \''.trim($alias_cate).'\')');
		$this->db->where('news.bl_active',1);
        return $this->db->get('news')->num_rows();
    }
    /*
     * @author : Tran Thi Kim Que
     * @method : get all news by category
     * @param  : $id_cat - ID category news
     *           $num - num of row each page
     *           $offset - position begin
     *           $arr_search - arrary condition search
     * @date   : 12/11/2012
     * @modify :
     */
    function getAllNewsByCat($alias_cat,$num,$offset,$arr_search=array()){
        $this->db->select('category_news.name_alias'.$this->_lang.' as name_alias,category_news.name'.$this->_lang.' as name,news.news_id,news.category_news_id,news.image,news.title'.$this->_lang.' as title, news.title_alias'.$this->_lang.' as alias, news.description'.$this->_lang.' as description,news.content'.$this->_lang.' as content,news.seo_keyword'.$this->_lang.' as seo_keyword,news.news_relate,news.nb_read,news.bl_home,like,news.dt_create');
        $this->db->join('category_news','news.category_news_id=category_news.category_news_id');
        $this->db->where('('.$this->db->dbprefix('category_news').'.name_alias=\''.trim($alias_cat).'\' OR '.$this->db->dbprefix('category_news').'.name_alias_en = \''.trim($alias_cat).'\')');
		$this->db->where('news.bl_active',1);
		$this->db->where('news.dt_public < ',date('Y-m-d'));
        $this->db->order_by('news.dt_public','DESC');
        return $this->db->get('news',$num,$offset)->result();
    }
    /*
     * @author : Tran Thi Kim Que
     * @method : get news by ID
     * @param  : $id_cat - ID category news
     *           $news_id - ID news
     * @date   : 12/11/2012
     * @modify :
     */
    function getNewById($news_id){
        $this->db->select('category_news.name_alias'.$this->_lang.' as name_alias,category_news.name'.$this->_lang.' as name,news.news_id,news.category_news_id,news.image,news.title'.$this->_lang.' as title, news.title_alias'.$this->_lang.' as alias, news.description'.$this->_lang.' as description,news.content'.$this->_lang.' as content,news.seo_keyword'.$this->_lang.' as seo_keyword,news.news_relate,news.nb_read,like,news.dt_create');
		$this->db->join('category_news','news.category_news_id=category_news.category_news_id');
        $this->db->where('news.bl_active',1);
        $this->db->where('news.news_id',$news_id);
        return $this->db->get('news')->row();
    }
    /*
     * @author : Tran Thi Kim Que
     * @method : get all home news 
     * @param  : 
     * @date   : 12/11/2012
     * @modify :
     */
    function getNewsHome(){
		 $this->db->select('category_news.name_alias'.$this->_lang.' as name_alias,news.news_id,news.category_news_id,news.image,news.title'.$this->_lang.' as title, news.title_alias'.$this->_lang.' as alias, news.description'.$this->_lang.' as description');
		$this->db->join('category_news','news.category_news_id=category_news.category_news_id');
        $this->db->where('news.bl_active',1);
        $this->db->where('news.bl_home',1);
        $this->db->order_by('news.dt_create','DESC');
        return $this->db->get('news',5)->result();
    }
	 /*
     * @author : Tran Thi Kim Que
     * @method : get Alias Category news
     * @param  : 
     * @date   : 12/11/2012
     * @modify :
     */
	  function getAliasCategory($cid){
        $this->db->select('name_alias'.$this->_lang);
		$this->db->where('category_news_id',$cid);
        return $this->db->get('category_news')->row()->name_alias.$this->_lang;
    }
	 function getIdFromAliasCategory($alias_cat){
        $this->db->select('category_news_id');
		$this->db->where('name_alias'.$this->_lang,$alias_cat);
        return $this->db->get('category_news')->row()->category_news_id;
    }
	 /*
     * @author : Tran Thi Kim Que
     * @method : get Name Category news
     * @param  : 
     * @date   : 12/11/2012
     * @modify :
     */
	 function getNameCategory($cid){
        $this->db->select('name'.$this->_lang);
		$this->db->where('category_news_id',$cid);
        return $this->db->get('category_news')->row()->name.$this->_lang;
    }
	
	 /*
     * @author : Tran Thi Kim Que
     * @method : get Three Hot News
     * @param  : 
     * @date   : 12/11/2012
     * @modify :
     */
	 function getThreeHotNews($cid,$new_id){
        $this->db->select('category_news.name_alias'.$this->_lang.' as name_alias,news.news_id,news.category_news_id,like,news.image,news.title'.$this->_lang.' as title, news.title_alias'.$this->_lang.' as alias, news.description'.$this->_lang.' as description');
		$this->db->join('category_news','news.category_news_id=category_news.category_news_id');
		$this->db->where('category_news.category_news_id',$cid);
		$this->db->where('news_id <> ',$new_id);
		$this->db->order_by('news.dt_create','DESC');
		$this->db->where('news.bl_active',1);
        return $this->db->get('news',3)->result();
    }
	
	/*
     * @author : Tran Thi Kim Que
     * @method : get Name Category news
     * @param  : 
     * @date   : 12/11/2012
     * @modify :
     */
	 
	  function getOtherListNews($alias_cat,$num,$offset){
        $this->db->select('category_news.name_alias'.$this->_lang.' as name_alias,news.news_id,news.category_news_id,news.image,news.title'.$this->_lang.' as title, like,news.title_alias'.$this->_lang.' as alias,news.dt_create');
		$this->db->join('category_news','news.category_news_id=category_news.category_news_id');
		$this->db->where('category_news.name_alias'.$this->_lang,$alias_cat);
		$this->db->order_by('news.dt_create','DESC');
		$this->db->where('news.bl_active',1);
        return $this->db->get('news',$num,$offset)->result();
    }
	
	/*
     * @author : Tran Thi Kim Que
     * @method : get Name Category news
     * @param  : 
     * @date   : 12/11/2012
     * @modify :
     */
	 
	  function getOtherTravelNews($alias_cat,$arr_list_id,$num,$offset){
        $this->db->select('category_news.name_alias'.$this->_lang.' as name_alias,news.news_id,news.category_news_id,news.image,news.title'.$this->_lang.' as title, like,news.title_alias'.$this->_lang.' as alias,news.dt_create');
		$this->db->join('category_news','news.category_news_id=category_news.category_news_id');
		$this->db->where('category_news.name_alias'.$this->_lang,$alias_cat);
		$this->db->where('news.bl_active',1);
		if(is_null($arr_list_id))
			$this->db->where_not_in('news.news_id', $arr_list_id);
		$this->db->order_by('news.dt_create','DESC');
		$this->db->where('news.bl_active',1);
        return $this->db->get('news',$num,$offset)->result();
    }
	
	 /*
     * @author : Tran Thi Kim Que
     * @method : get Name Category news
     * @param  : 
     * @date   : 12/11/2012
     * @modify :
     */
	 
	 function getOtherNews($cid,$new_id){
        $this->db->select('category_news.name_alias'.$this->_lang.' as name_alias,news.news_id,news.category_news_id,like,news.image,news.title'.$this->_lang.' as title, news.title_alias'.$this->_lang.' as alias,dt_create');
		$this->db->join('category_news','news.category_news_id=category_news.category_news_id');
		$this->db->where('category_news.category_news_id',$cid);
		$this->db->where('news.bl_active',1);
		$this->db->where('news.news_id <> ',$new_id);
		$this->db->order_by('news.dt_create','DESC');
        return $this->db->get('news',13)->result();
    }

    /*
     * @author : Tran Thi Kim Que
     * @method : get all news from all category
     * @param  : 
     * @date   : 12/11/2012
     * @modify :
     */
	function getAllNews(){
        $this->db->select('news_id,category_news_id,image,title'.$this->_lang.' as title,description'.$this->_lang.' as description,content'.$this->_lang.' as content,like,seo_keyword'.$this->_lang.' as seo_keyword,news_relate,nb_read,bl_home,dt_create');
        $this->db->where('bl_active',1);
        $this->db->order_by('dt_create','DESC');
        return $this->db->get('news')->result();
    }
    
    /*
     * @author : Tran Thi Kim Que
     * @method : get all news from all category
     * @param  : 
     * @date   : 3/12/2012
     * @modify :
     */
	function getNewsFromDate($cond_search=''){
        $this->db->select('category_news.name_alias'.$this->_lang.' as name_alias,category_news.name'.$this->_lang.' as name,news.news_id,news.category_news_id,news.image,like,news.title'.$this->_lang.' as title, news.title_alias'.$this->_lang.' as alias, news.description'.$this->_lang.' as description,news.content'.$this->_lang.' as content,news.seo_keyword'.$this->_lang.' as seo_keyword,news.news_relate,news.nb_read,news.bl_home,news.dt_create');
        $this->db->join('category_news','news.category_news_id=category_news.category_news_id');
        $this->db->where("date(dt_create)",$cond_search);
		$this->db->where('news.bl_active',1);
        $this->db->order_by('news.dt_create','DESC');
        return $this->db->get('news',10,0)->result();
    }
    function updateNews($news_id,$data){
        $this->db->where('news_id',$news_id);
        if($this->db->update('news',$data)){
            return true;
        }else{
            return false;
        }
    }
	
	
	
}